#include <QtGui/QtGui>
#include <iostream>
#include "framework.h"
#include "TriMesh.h"
#include "debug.h"

Framework::Framework(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	readSettings();
	setCurrentFile("");

	m_pModel = NULL ;
}

void Framework::on_actionPoints_triggered()
{

	ui.centralWidget->getModel()->SetRenderingStyles(TriMesh::DRAW_POINTS);
}

void Framework::on_actionTriangle_triggered()
{

	ui.centralWidget->getModel()->SetRenderingStyles(TriMesh::DRAW_TRIANGLES);
}

void Framework::on_actionEdge_triggered()
{
	ui.centralWidget->getModel()->SetRenderingStyles(TriMesh::DRAW_EDGES);
}

Framework::~Framework()
{
	if(m_pModel)
	{
		delete m_pModel;
	    m_pModel = NULL; 
	}
}


void Framework::closeEvent(QCloseEvent *event)
{
		writeSettings();
		event->accept();
}


void Framework::on_actionOpen_triggered()
{

		QString fileName = QFileDialog::getOpenFileName(this,
			tr("Open point cloud from file"), ".",
			tr("OBJ files (*.obj)"));

		if (!fileName.isEmpty())
		{
			loadFile(fileName);
		}

}

void  Framework::on_actionSave_triggered()
{
	QString baseName = QFileInfo(m_curFile).baseName();
	QString fileName = QFileDialog::getSaveFileName(this,
		tr("Save model to file"), baseName,
		tr("OBJ files (*.obj)"));
	if (!fileName.isEmpty())
	{
		saveFile(fileName);	
	}
}

bool Framework::loadFile(const QString &fileName)
{
	QByteArray temp = fileName.toLatin1();
	//const char* fn = temp.data();

	QString ext = QFileInfo(fileName).suffix();
	if( ext=="obj" || ext=="OBJ")
	{
		if(m_pModel) delete m_pModel;
		m_pModel = TriMesh::read(fileName.toAscii());
		m_pModel->need_faces();
		m_pModel->need_normals();
		
		if(m_pModel->faces.size() ==0)
			m_pModel->SetRenderingStyles(TriMesh::DRAW_POINTS);
		else
			m_pModel->SetRenderingStyles(TriMesh::DRAW_EDGES);
			
		ui.centralWidget->setModel(m_pModel);
		ui.centralWidget->update();

		setCurrentFile(fileName);
		statusBar()->showMessage(tr("File loaded"), 2000);
		return true;
	}
	return false;
}

bool Framework::saveFile(const QString &fileName)
{
	QString ext = QFileInfo(fileName).suffix();

	if (ext!="obj" && ext!="OBJ")
		return false;

	m_pModel->write(fileName.toAscii());
	
	statusBar()->showMessage(tr("File saved"), 2000);
	return true;
}

void Framework::setCurrentFile(const QString &fileName)
{
	m_curFile = fileName;
	setWindowModified(false);

	setWindowTitle(tr("%1[*] - %2").arg(strippedName(fileName))
		.arg(tr("Framework")));
}

QString Framework::strippedName(const QString &fullFileName)
{
	return QFileInfo(fullFileName).fileName();
}

void Framework::on_actionAbout_triggered()
{
	QMessageBox::about(this, tr("About Framework"),
		tr("<h2>Framework 1.1</h2>"
		"<p>Copyright &copy; 2011 Software Inc."
		"<p>Framework is a small application that "
		"demonstrates QAction, QMainWindow, QMenuBar, "
		"QStatusBar, QTableWidget, QToolBar, and many other "
		"Qt classes."));
}

void Framework::readSettings()
{
	QSettings settings("Software Inc.", "Framework");

	restoreGeometry(settings.value("geometry").toByteArray());
}

void Framework::writeSettings()
{
	QSettings settings("Software Inc.", "Framework");

	settings.setValue("geometry", saveGeometry());
}
